package com.yuanchu.marketing.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-01-18 17:06
 */
@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null){
            HttpServletRequest request = attributes.getRequest();
            String tokenStr = request.getHeader("Authorization");
            if (tokenStr == null){
                return;
            }
            String token = tokenStr.split(" ")[1];
            requestTemplate.header("Authorization",tokenStr);
        }
    }
}

